#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
int main(int argc,char*argv[])
{
if(argc!=3)
   {
    cout<<"please:"<<"./client serverip serverport"<<endl;
    exit(1);
   }

   uint16_t port=stoi(argv[2]);
   string ip=argv[1];

struct sockaddr_in server;
int len=sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(port);
inet_pton(AF_INET,ip.c_str(),&(server.sin_addr));

while(1)
{
  int cnt=5;
  int isreconnect=false;
  int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{cout<<"套接字绑定失败"<<endl;}
 cout<<"套接字绑定成功"<<endl;


do{


int n=connect(sockfd,(const sockaddr*)&server,len);
if(n<0)
{
isreconnect=true;
cnt--;
cout<<"connect error......"<<endl;
sleep(2);
}
else
{break;}


}while(cnt&&isreconnect);

if(cnt==0)
{
cout<<"user offline...."<<endl;
break;
}


string message;


//while(1)
//{
  cout<<"please enter#"<<endl;
  getline(cin,message);

  write(sockfd,message.c_str(),message.size());



char buffer[1024];

  int n=read(sockfd,buffer,sizeof(buffer));
  if(n>0)
    {
     

     buffer[n]=0;
     cout<<buffer<<endl;




    }
  
 
//}
 close(sockfd);


}

return 0;






}